package com.hnzq.cloud.sendassembly.controller;


import com.hnzq.cloud.IO.AddCarIO;
import com.hnzq.cloud.IO.entity.FreCar;
import com.hnzq.cloud.api.FreCarApi;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.sendassembly.service.FreCarService;
import com.hnzq.cloud.util.FreException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tiangle
 * @since 2022-02-07
 */
@RestController
@RequestMapping("/freCar")
public class FreCarController implements FreCarApi {
    @Autowired
    private FreCarService freCarService;


    @Override
    public Result getByUserId(Integer id) throws FreException {
        FreCar byUserId = freCarService.getByUserId(id);
        return Result.newInstance().setData(byUserId).setCode(200);
    }

    @Override
    public Result<List<FreCar>> getAll() {
        List<FreCar> list = freCarService.list();
        return Result.newInstance().setData(list).setCode(200);
    }

    @Override
    public Result addCar(AddCarIO addCarIO) {
        if (freCarService.addFreCar(addCarIO)) {
            return Result.newInstance().setCode(200).setMessage("添加成功");
        }
        return Result.newInstance().setCode(500).setMessage("添加失败");
    }
    @GlobalTransactional
    @Override
    public Result editCar(AddCarIO addCarIO) {
        if (freCarService.editFreCar(addCarIO)) {
            return Result.newInstance().setCode(200).setMessage("修改成功");
        }
        return Result.newInstance().setCode(500).setMessage("修改失败");
    }

    @Override
    public Result deleteCar(Integer id) {
        if (freCarService.deleteFreCar(id)) {
            return Result.newInstance().setCode(200).setMessage("删除成功");
        }
        return Result.newInstance().setCode(500).setMessage("删除失败");

    }
}

